package cn.school.demo.ui.set_love

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import cn.school.demo.MyApplication
import cn.school.demo.pojo.Hobby
import cn.school.demo.pojo.Result
import cn.school.demo.repository.SetLoveRepository
import com.blankj.utilcode.util.LogUtils

class SetLoveViewModel : ViewModel() {
    private val _isEdit = MutableLiveData<Boolean>()
    val isEdit: MutableLiveData<Boolean>
        get() = _isEdit

    private var _tabs: LiveData<MutableList<Hobby>>
    val tabs: LiveData<MutableList<Hobby>>
        get() = _tabs

    private var _tabTitle: LiveData<MutableList<Hobby>>
    val tabTitle: LiveData<MutableList<Hobby>>
        get() = _tabTitle

    private lateinit var _save: LiveData<Result>
    val save: LiveData<Result> get() = _save

    init {
        _isEdit.value = false
        _tabs = getTabTitles()
        _tabTitle = getAllTabTitles()
    }

    private fun getTabTitles(): LiveData<MutableList<Hobby>> {
        //http请求获取数据
        return SetLoveRepository().getUserLove(MyApplication.user!!.sfNumber)
    }

    private fun getAllTabTitles(): LiveData<MutableList<Hobby>> {
        //http请求获取数据
        return SetLoveRepository().getAllLove(MyApplication.user!!.sfNumber)
    }

    fun addTab(position: Int): Int {
        _tabs.value!!.add(_tabTitle.value!![position])
        _tabTitle.value!!.removeAt(position)
        return _tabTitle.value!!.size
    }

    fun removeTab(position: Int): Int {
        when (_tabs.value!![position].hobbyName) {
            "推荐" -> {}
            "要闻" -> {}
            "党史" -> {}
            "法律" -> {}
            "消息" -> {}
            else -> {
                _tabTitle.value!!.add(_tabs.value!![position])
                _tabs.value!!.removeAt(position)
            }
        }
        return _tabTitle.value!!.size
    }

    fun saveUserTabs() {
        //http发送标签到后端
        LogUtils.e(_tabs.value.toString(), _tabs.value!!.size)
        _save = SetLoveRepository().saveUserLove(
            MyApplication.user!!.sfNumber,
            _tabs.value!!
        )
    }
}